const models = require("../db/mysqldb/index");
const { Op, where } = require("sequelize");

class DataSystemPostModel {
  /**
   * @description 通过 post_id 查找数据，不包含删除数据
   * @param {string} post_id
   * @returns
   */
  static async findBySystemPostId({ post_id }) {
    return await models.sys_post.findOne({
      where: {
        post_id,
      },
    });
  }
  /**
   * @description 通过 post_name 查找数据，不包含删除数据
   * @param {string} post_name
   * @returns
   */
  static async findBySystemPostName({ post_name }) {
    return await models.sys_post.findOne({
      where: {
        post_name,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 post_code 查找数据，不包含删除数据
   * @param {string} post_code
   * @returns
   */
  static async findBySystemPostCode({ post_code }) {
    return await models.sys_post.findOne({
      where: {
        post_code,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 post_name id 查找数据，不包含删除数据
   * @param {string} post_name
   * @param {string} post_id
   * @returns
   */
  static async findBySystemPostNameNeId({ post_name, post_id }) {
    return await models.sys_post.findOne({
      where: {
        post_name,
        del_flag: "0",
        post_id: {
          [Op.ne]: post_id,
        },
      },
    });
  }
  /**
   * @description 通过 post_code id 查找数据，不包含删除数据
   * @param {string} post_code
   * @param {string} post_id
   * @returns
   */
  static async findBySystemPostCodeId({ post_code, post_id }) {
    return await models.sys_post.findOne({
      where: {
        post_code,
        del_flag: "0",
        post_id: {
          [Op.ne]: post_id,
        },
      },
    });
  }
  /**
   * @description create
   * @param {String} post_name
   * @param {String} post_code
   * @param {Number} post_sort
   * @param {Number} status
   * @param {String} remark
   * @param {String} op_user_id
   * @returns
   */
  static async create({
    post_name,
    post_code,
    post_sort,
    status,
    remark,
    op_user_id,
  }) {
    return await models.sys_post.create({
      post_name,
      post_code,
      post_sort,
      status,
      remark,
      create_by: op_user_id,
      create_date: Date.now(),
    });
  }
  /**
   * @description 更新
   * @param {String} post_name
   * @param {String} post_code
   * @param {Number} post_sort
   * @param {Number} status
   * @param {String} remark
   * @param {String} op_user_id
   * @param {Number} post_id
   * @returns
   */
  static async update({
    post_name,
    post_code,
    post_sort,
    status,
    remark,
    op_user_id,
    post_id,
  }) {
    return await models.sys_post.update(
      {
        post_name,
        post_code,
        post_sort,
        status,
        remark,
        update_by: op_user_id,
        update_time: Date.now(),
      },
      {
        where: { post_id },
      }
    );
  }
  /**
   * @description 删除
   * @param {*} op_user_id
   * @param {*} post_id
   * @returns
   */
  static async delete({ op_user_id, post_id }) {
    return await models.sys_post.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: { post_id },
      }
    );
  }
  /**
   * @description 批量删除
   * @param {*} op_user_id
   * @param {*} ids
   * @returns
   */
  static async deleteByIds({ op_user_id, ids }) {
    return await models.sys_post.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: {
          post_id: {
            [Op.in]: ids,
          },
        },
      }
    );
  }
  /**
   * @description 获取岗位信息
   * @param {String} post_name
   * @param {String} post_code
   * @param {String} status
   * @param {String} limit
   * @param {String} size
   * @returns
   */
  static async list({
    limit,
    size,
    post_name,
    post_code,
    status,
    data_scope = "",
  }) {
    let querySql = "";
    if (post_name) {
      querySql += ` and post_name like '%${post_name}%'`;
    }
    if (post_code) {
      querySql += ` and post_code like '%${post_code}%'`;
    }
    if (status) {
      querySql += ` and status = ${status}`;
    }
    let sql = `SELECT
        post_id,
        post_name,
        post_code,
        post_sort,
        status,
        remark,
        create_time
      FROM
        sys_post WHERE del_flag = '0' ${querySql} ${data_scope}
      ORDER BY
        post_sort ASC,
        update_time desc,create_time desc
        limit ${(limit - 1) * size}, ${size}`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    const count = await models.sequelize.query(
      `SELECT count(1) as count FROM
      sys_post WHERE del_flag = '0' ${querySql} ${data_scope};`,
      { replacements: ["active"], type: models.sequelize.QueryTypes.SELECT }
    );
    return {
      result,
      count,
    };
  }
  /**
   * @description 获取所有岗位信息
   * @returns
   */
  static async all({ data_scope = "" }) {
    let sql = `SELECT
        post_id,
        post_name,
        post_code,
        post_sort,
        status,
        remark,
        create_time
      FROM
        sys_post WHERE del_flag = '0' ${data_scope}
      ORDER BY
        post_sort ASC,
        create_time DESC,
        update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
  /**
   * @description 获取岗位信息
   * @param {String} post_name
   * @param {String} post_code
   * @param {String} status
   * @returns
   */
  static async findList({ post_name, post_code, status, data_scope }) {
    let querySql = "";
    if (post_name) {
      querySql += ` and post_name like '%${post_name}%'`;
    }
    if (post_code) {
      querySql += ` and post_code like '%${post_code}%'`;
    }
    if (status) {
      querySql += ` and status = '${status}'`;
    }
    let sql = `SELECT
    post_id,
    post_name,
    post_code,
    post_sort,
    status,
    remark,
    create_time
  FROM
    sys_post WHERE del_flag = '0' ${data_scope} ${querySql}
  ORDER BY
    post_sort ASC,
    create_time DESC,
    update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
}
module.exports = {
  DataSystemPostModel,
};
